<template>
	<view class="container">
		<!-- 导航栏 -->
		<view class="nav-bar" :style="{ paddingTop: safeAreaInsets.top + 'px' }">
			<view class="nav-left" @click="goBack">
				<uni-icons type="left" size="24" color="#333"></uni-icons>
			</view>
			<view class="nav-title">更多商品</view>
		</view>

		<!-- 商品列表 -->
		<scroll-view scroll-y class="goods-list" :style="{ height: scrollViewHeight }" @scrolltolower="loadMore">
			<view class="goods-box">
				<view class="goods-item" v-for="(item, index) in goodsList" :key="index" @click="gotoDetail(item)">
					<image class="goods-img" :src="'http://localhost:8080' + item.goodsImage1" mode="aspectFill" />
					<view class="goods-info">
						<text class="goods-name">{{ item.goodsName }}</text>
						<text class="goods-price">￥{{ item.goodsPrice }}</text>
					</view>
				</view>
			</view>
			<view class="load-more">
				{{ loading ? '正在加载...' : hasMore ? '上拉加载更多' : '没有更多了' }}
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 初始化安全区域数据
				safeAreaInsets: {
					top: 0,
					bottom: 0
				},
				goodsList: [],
				pageParams: {
					page: 1,
					pageSize: 10,
					total: 0
				},
				loading: false,
				hasMore: true
			}
		},
		computed: {
			// 动态计算滚动区域高度
			scrollViewHeight() {
				return `calc(100vh - ${this.safeAreaInsets.top + 88}px)`
			}
		},
		async onLoad() {
			try {
				const systemInfo = await uni.getSystemInfo()
				// 确保 safeAreaInsets 有默认值
				this.safeAreaInsets = {
					top: systemInfo.safeArea ? systemInfo.safeArea.top : systemInfo.statusBarHeight,
					bottom: systemInfo.safeArea ? systemInfo.safeArea.bottom : 0
				}
			} catch (error) {
				console.error('获取系统信息失败:', error)
			}
			this.loadGoods()
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},

			async loadGoods() {
				if (this.loading || !this.hasMore) return

				this.loading = true
				try {
					const {
						data
					} = await uni.request({
						url: 'http://localhost:8080/goods/goods/list',
						data: {
							page: this.pageParams.page,
							pageSize: this.pageParams.pageSize
						},
						header: {
							'Authorization': uni.getStorageSync('token')
						}
					})

					if (data.code === 200) {
						this.goodsList = [...this.goodsList, ...data.rows]
						this.pageParams.total = data.total
						// 判断是否还有更多数据
						this.hasMore = this.pageParams.page * this.pageParams.pageSize < data.total
					}
				} catch (error) {
					console.error('加载商品失败:', error)
				} finally {
					this.loading = false
				}
			},

			loadMore() {
				if (this.hasMore) {
					this.pageParams.page++
					this.loadGoods()
				}
			},

			gotoDetail(item) {
				uni.navigateTo({
					url: `/pages/shop-details/shop-details?goodsId=${item.goodsId}`
				})
			}
		}
	}
</script>

<style>
	.container {
		background-color: #FFF8F0;
		min-height: 100vh;
	}

	.nav-bar {
		position: sticky;
		top: 0;
		z-index: 999;
		display: flex;
		align-items: center;
		padding: 0 20rpx;
		min-height: 88rpx;
		background: white;
		border-bottom: 1rpx solid #eee;
	}

	.nav-left {
		padding: 10rpx;
	}

	.nav-title {
		flex: 1;
		text-align: center;
		font-size: 34rpx;
		font-weight: bold;
	}

	.goods-list {
		width: 96%;
		margin: 0 auto;
		padding: 20rpx 0;
	}

	.goods-box {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}

	.goods-item {
		width: 49%;
		background: white;
		border-radius: 10rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	}

	.goods-img {
		width: 100%;
		height: 350rpx;
	}

	.goods-info {
		padding: 15rpx;
	}

	.goods-name {
		/* 	font-size: 28rpx;
		color: #333;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
		line-height: 1.4; */
		color: #333;
		font-size: 28rpx;
		display: block;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		margin-bottom: 5px;
	}

	.goods-price {
		display: block;
		color: #e4393c;
		font-size: 32rpx;
		font-weight: bold;
		margin-top: 10rpx;
	}

	.load-more {
		text-align: center;
		color: #999;
		font-size: 26rpx;
		padding: 30rpx 0;
	}
</style>
